package com.ks.api.controller;

import com.ks.api.entity.KsUserCostSummary;
import com.ks.api.service.KsUserCostSummaryService;
import com.ks.api.utils.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户成本汇总控制器
 */
@RestController
@RequestMapping("/cost")
@RequiredArgsConstructor
@Slf4j
public class KsUserCostSummaryController {

    private final KsUserCostSummaryService ksUserCostSummaryService;

    /**
     * 查询所有用户成本汇总数据
     */
    @GetMapping("/all")
    public Map<String, Object> findAllCostSummaries(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false, value = "nickname") String nickName,
            @RequestParam(required = false, value = "nickId") String nickId) {
        return ksUserCostSummaryService.findCostSummariesWithUserInfo(page, size, startTime, endTime, nickName, nickId);
    }
    /**
     * 分页查询用户成本汇总数据
     */
    @GetMapping("/list")
    public R<Map<String, Object>> listCostSummaries(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String nickId,
            @RequestParam(required = false) String nickName) {
        try {
            Map<String, Object> result = ksUserCostSummaryService.findCostSummaries(page, size, nickId, nickName);
            return R.ok(result);
        } catch (Exception e) {
            log.error("查询用户成本汇总数据失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 事件搜索接口
     */
    @GetMapping("/search")
    public R<Map<String, Object>> searchEvents(
            @RequestParam(required = false) String nickname,
            @RequestParam(required = false) String nickId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Map<String, Object> result = ksUserCostSummaryService.searchEvents(page, size, nickId, nickname);
            return R.ok(result);
        } catch (Exception e) {
            log.error("查询事件数据失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }
} 